package com.ruoyi.web.controller.app.service;

import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.framework.web.exception.base.XException;
import com.ruoyi.sj.domain.GiveLike;
import com.ruoyi.sj.mapper.GiveLikeMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Map;

/**
 * @ClassName MyGiveLikeService
 * @Description TODO
 * @Author tyc@yanyu
 * @Date 2018/12/12 0012 15:22
 **/
@Service
public class AppGiveLikeService {
    @Autowired
    private GiveLikeMapper giveLikeMapper;

    /**
     * 点赞
     * @param map
     * @return
     */
    public Object giveLike(Map<String, String> map) {
        if (StringUtils.isBlank(map.get("memberId"))||"null".equals(map.get("memberId"))){
            throw new XException("用户id不能为空！");
        }
        if (StringUtils.isBlank(map.get("dynamicId"))||"null".equals(map.get("dynamicId"))){
            throw new XException("动态id不能为空");
        }

        GiveLike giveLike = new GiveLike();
        giveLike.setMemberId(Long.valueOf(map.get("memberId")));
        giveLike.setDynamicId(Long.valueOf(map.get("dynamicId")));
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        giveLike.setCreateDate(sdf.format(new Date()));
        giveLike.setStatus("1");
        return giveLikeMapper.insertGiveLike(giveLike);
    }
    /**
     *取消点赞
     */
    public Object cancleGiveLike(Map<String, String> map) {
        if (StringUtils.isBlank(map.get("memberId"))||"null".equals(map.get("memberId"))){
            throw new XException("用户id不能为空！");
        }
        if (StringUtils.isBlank(map.get("dynamicId"))||"null".equals(map.get("dynamicId"))){
            throw new XException("动态id不能为空");
        }

        GiveLike giveLike = new GiveLike();
        giveLike.setMemberId(Long.valueOf(map.get("memberId")));
        giveLike.setDynamicId(Long.valueOf(map.get("dynamicId")));
        String status = giveLikeMapper.queryStatusById(giveLike);
        if (StringUtils.isBlank(status)||"null".equals(status)){
            throw new XException("未点赞");
        }
        if ("2".equals(status)){
            return new XException("未点赞");
        }else {
            return giveLikeMapper.changeStatus(giveLike);
        }

    }

    public Long getNumbyId(String dynamicId) {
        return giveLikeMapper.getNum(dynamicId);
    }
}
